home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicColorChooserUI.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  297 lines

  1. /*
  2.  * @(#)BasicColorChooserUI.java    1.11 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.preview.*;
  25. import com.sun.java.swing.event.*;
  26. import com.sun.java.swing.plaf.ComponentUI;
  27. import com.sun.java.swing.plaf.ColorChooserUI;
  28. import java.util.*;
  29. import java.awt.*;
  30. import java.awt.image.*;
  31. import java.awt.event.*;
  32. import java.beans.PropertyChangeEvent;
  33. import java.beans.PropertyChangeListener;
  34. import java.io.Serializable;
  35.  
  36.  
  37. /**
  38.  * Provides the basic look and feel for a JColorChooser.
  39.  * <p>
  40.  * @version 1.11 02/02/98
  41.  * @author Tom Santos
  42.  */
  43.  
  44. public class BasicColorChooserUI extends ColorChooserUI implements PropertyChangeListener
  45. {
  46.     JColorChooser chooser;
  47.     JTabbedPane tabbedPane;
  48.     Hashtable panels;
  49.  
  50.     public static ComponentUI createUI(JComponent x) {
  51.     return new BasicColorChooserUI();
  52.     }
  53.  
  54.     public void installUI( JComponent c ) {
  55.         super.installUI( c );
  56.     installDefaults( c );
  57.  
  58.         chooser = (JColorChooser)c;
  59.     tabbedPane = new JTabbedPane();
  60.     panels = new Hashtable();
  61.  
  62.         chooser.setLayout( createSinglePaneLayout() );
  63.  
  64.         ColorChooserPanel panel = new BasicHSVChooserPanel();
  65.     chooser.addChooserPanel( "HSV", panel );
  66.  
  67.     panel = new BasicRGBChooserPanel();
  68.     chooser.addChooserPanel( "RGB", panel );
  69.  
  70.     installListeners( c );
  71.     }
  72.  
  73.     public void uninstallUI( JComponent c ) {
  74.     chooser.removeChooserPanel( "HSV" );
  75.  
  76.     panels = null;
  77.     chooser = null;
  78.     tabbedPane = null;
  79.  
  80.     uninstallDefaults( c );
  81.     uninstallListeners( c );
  82.     }
  83.  
  84.     public Dimension getPreferredSize( JComponent c ) {
  85.         Dimension result;
  86.  
  87.         if ( panels.size() == 1 ) {
  88.         result = ((ColorChooserPanel)panels.elements().nextElement()).getPreferredSize();
  89.     }
  90.     else if ( panels.size() > 1 ) {
  91.         return tabbedPane.getPreferredSize();
  92.     }
  93.     else {
  94.         result = new Dimension( 0, 0 );
  95.     }
  96.  
  97.     return result;
  98.     }
  99.  
  100.     protected void installDefaults(JComponent c) {
  101.         LookAndFeel.installColorsAndFont(c, "ColorChooser.background", 
  102.                                               "ColorChooser.foreground",
  103.                                               "ColorChooser.font");
  104.     }
  105.  
  106.     protected void uninstallDefaults(JComponent c) {
  107.     }
  108.  
  109.     protected void installListeners( JComponent c ) {
  110.     chooser.addPropertyChangeListener( this );
  111.     }
  112.  
  113.     protected void uninstallListeners( JComponent c ) {
  114.     chooser.removePropertyChangeListener( this );
  115.     }
  116.  
  117.     // This is only used in propertyChange()
  118.     boolean settingAllPanels = false;
  119.  
  120.     public void propertyChange(PropertyChangeEvent e) {
  121.     if ( e.getPropertyName().equals( ColorChooserPanel.COLOR_PROPERTY ) && !settingAllPanels ) {
  122.         if ( chooser.getColor() != e.getNewValue() ) {
  123.             chooser.setColor( (Color)e.getNewValue() );
  124.         }
  125.     }
  126.     else if ( e.getPropertyName().equals( JColorChooser.COLOR_PROPERTY ) ) {
  127.         Enumeration elements = panels.elements();
  128.         settingAllPanels = true;
  129.         while ( elements.hasMoreElements() ) {
  130.             ColorChooserPanel panel = (ColorChooserPanel)elements.nextElement();
  131.             if ( panel.getColor() != e.getNewValue() ) {
  132.             panel.setColor( (Color)e.getNewValue() );
  133.         }
  134.         }
  135.         settingAllPanels = false;
  136.     }
  137.     }
  138.  
  139.     /**
  140.      * Adds a color chooser panel to the color chooser.
  141.      *
  142.      * @param name   a string that specifies the name of the panel
  143.      */
  144.     public void addChooserPanel( String name, ColorChooserPanel panel ) {
  145.         if ( panel != null && name != null ) {
  146.         panel.installChooserPanel();
  147.  
  148.         if ( panels.size() == 0 && chooser.getColor() == null ) {
  149.             chooser.setColor( panel.getColor() );
  150.         }
  151.         else {
  152.             panel.setColor( chooser.getColor() );
  153.         }
  154.  
  155.         panels.put( name, panel );
  156.         panel.addPropertyChangeListener( this );
  157.  
  158.         if ( panels.size() == 2 ) {
  159.             switchToMultiplePanelSetup();
  160.         }
  161.         else if ( panels.size() <= 1 ) {
  162.             chooser.add( panel, "Center" );
  163.         }
  164.         else {
  165.             JPanel centeredPanel = new JPanel();
  166.         centeredPanel.setLayout( new CenterLayout() );
  167.         centeredPanel.add( panel );
  168.             tabbedPane.addTab( name, centeredPanel );
  169.         }
  170.     }
  171.     }
  172.  
  173.     /**
  174.      * Removes the ColorChooserPanel specified by "name".
  175.      *
  176.      * @param name   a string that specifies the panel to be removed
  177.      */
  178.     public ColorChooserPanel removeChooserPanel( String name ) {
  179.         ColorChooserPanel panel = null;
  180.  
  181.         if ( name != null ) {
  182.         panel = (ColorChooserPanel)panels.remove( name );
  183.  
  184.         if ( panels.size() > 1 ) {
  185.             tabbedPane.removeTabAt( tabbedPane.indexOfTab( name ) );
  186.         }
  187.         else if ( panels.size() == 0 ) {
  188.             chooser.remove( panel );
  189.         }
  190.         // If we are brought down to 1 panel don't do remove anything from
  191.         // anywhere.  We'll just let switchToSinglePanelSetup() do what's needed.
  192.  
  193.         panel.uninstallChooserPanel();
  194.         panel.removePropertyChangeListener( this );
  195.  
  196.         if ( panel != null && panels.size() == 1 ) {
  197.             switchToSinglePanelSetup();
  198.         }
  199.     }
  200.  
  201.         return panel;
  202.     }
  203.  
  204.  
  205.     /**
  206.      * Reorganizes to show multiple ColorChooserPanels.  It uses a tabbed pane
  207.      * show the multiple panels.
  208.      */
  209.     protected void switchToMultiplePanelSetup() {
  210.         chooser.setLayout( createTabbedPaneLayout() );
  211.     
  212.     Enumeration keys = panels.keys();
  213.  
  214.     while ( keys.hasMoreElements() ) {
  215.         String key = (String)keys.nextElement();
  216.         chooser.remove( (Component)panels.get( key ) );
  217.         JPanel panel = new JPanel();
  218.         panel.setLayout( new CenterLayout() );
  219.         panel.add( (Component)panels.get( key ) );
  220.         tabbedPane.addTab( key, panel );
  221.     }
  222.  
  223.     chooser.add( tabbedPane, "Center" );
  224.     }
  225.  
  226.  
  227.     protected void switchToSinglePanelSetup() {
  228.         chooser.remove( tabbedPane );
  229.  
  230.     for ( int i = 0; i < panels.size(); ++i ) {
  231.         tabbedPane.removeTabAt( 0 );
  232.     }
  233.  
  234.         chooser.setLayout( createSinglePaneLayout() );
  235.  
  236.     Enumeration elements = panels.elements();
  237.  
  238.     if ( elements.hasMoreElements() ) {
  239.         chooser.add( (Component)elements.nextElement(), "Center" );
  240.     }
  241.     }
  242.  
  243.     protected LayoutManager createSinglePaneLayout() {
  244.         return new CenterLayout();
  245.     }
  246.  
  247.     protected LayoutManager createTabbedPaneLayout() {
  248.         return new BorderLayout();
  249.     }
  250.  
  251.     /**
  252.      * Center-positioning layout manager.
  253.      * <p>
  254.      * Warning: serialized objects of this class will not be compatible with
  255.      * future swing releases.  The current serialization support is appropriate
  256.      * for short term storage or RMI between Swing1.0 applications.  It will
  257.      * not be possible to load serialized Swing1.0 objects with future releases
  258.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  259.      * baseline for the serialized form of Swing objects.
  260.      */
  261.     public static class CenterLayout implements LayoutManager, Serializable {
  262.         public void addLayoutComponent(String name, Component comp) { }
  263.         public void removeLayoutComponent(Component comp) { }
  264.  
  265.         public Dimension preferredLayoutSize( Container container ) {
  266.         Component c = container.getComponent( 0 );
  267.         if ( c != null ) {
  268.             return c.getPreferredSize();
  269.         }
  270.         else {
  271.             return new Dimension( 0, 0 );
  272.         }
  273.         }
  274.  
  275.         public Dimension minimumLayoutSize(Container cont) {
  276.             return preferredLayoutSize(cont);
  277.         }
  278.  
  279.         public void layoutContainer(Container container) {
  280.         try {
  281.             Component c = container.getComponent( 0 );
  282.         
  283.         c.setSize( c.getPreferredSize() );
  284.         Dimension size = c.getSize();
  285.         Dimension containerSize = container.getSize();
  286.         int componentLeft = (containerSize.width / 2) - (size.width / 2);
  287.         int componentTop = (containerSize.height / 2) - (size.height / 2);
  288.  
  289.         c.setBounds( componentLeft, componentTop, size.width, size.height );
  290.         }
  291.         catch( Exception e ) {
  292.         }
  293.         }
  294.     }
  295. }
  296.  
  297.